求一个int类型数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1
数据范围:数据组数:,
进阶:时间复杂度:,空间复杂度:
while True: try: print(len(sorted(str(bin(int(input())))[2:].split('0'),reverse=True)[0])) except: break
input_str = input() input_2_str = bin(int(input_str)) input_2_str = input_2_str[2:] longgest = 0 temp = 0 for i in input_2_str: if(i == "1"): temp = temp +1 else: if(temp != 0): if(temp > longgest): longgest = temp temp = 0 else: continue #这里注意一个边界条件,就是如果最后一直都是1,那么最后不会触发else的情况,导致无法更新longgest #所以循环结束后,还需要再判断一次是否保存 if(temp > longgest): longgest = temp print(longgest)
#法一: n = bin(int(input()))[2:].split('0') print(len(sorted(n)[-1])) ''' 法二: temp = 0 lenth = 0 for i in n: if i == '1': temp += 1 else: temp = 0 lenth = max(lenth,temp) print(lenth) '''
n=int(input()) n=bin(n) l=(n[2:].split('0')) count=[] for i in l: count.append(len(i)) print(max(count))
a=int(input()) a=bin(a) a=str(a) b=[] for i in range (0,len(a)): cc=0 for j in range(i+1, len(a)): if a[i]=='1': if a[j]=='1': cc=cc+1 else: break b.append(cc+1) if max(b)==0: print(1) else: print(max(b))
while True: try: data = int(input()) data = bin(data)[2:] res = 0 max_len = 0 for x in data: if x=='1': res+=1 max_len = max(max_len, res) else: res=0 print(max(max_len, res)) except: break